<template>
  <button 
    :class="[
      'bx-button',
      'bx-glass',
      `bx-button--${type}`,
      `bx-button--${size}`,
      {
        'bx-button--loading': loading,
        'bx-button--disabled': disabled,
        'bx-button--block': block
      }
    ]"
    :disabled="disabled || loading"
    @click="handleClick"
  >
    <!-- 加载状态 -->
    <span v-if="loading" class="bx-button__loading">
      <svg class="bx-button__loading-icon" viewBox="0 0 24 24">
        <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2" fill="none" stroke-dasharray="31.416" stroke-dashoffset="31.416">
          <animate attributeName="stroke-dasharray" dur="2s" values="0 31.416;15.708 15.708;0 31.416" repeatCount="indefinite"/>
          <animate attributeName="stroke-dashoffset" dur="2s" values="0;-15.708;-31.416" repeatCount="indefinite"/>
        </circle>
      </svg>
    </span>

    <!-- 图标 -->
    <span v-if="icon && !loading" class="bx-button__icon">
      <component :is="icon" />
    </span>

    <!-- 按钮文本 -->
    <span v-if="$slots.default || text" class="bx-button__text">
      <slot>{{ text }}</slot>
    </span>
  </button>
</template>

<script>
export default {
  name: 'BxButton',
  props: {
    type: {
      type: String,
      default: 'default',
      validator: (value) => ['default', 'primary', 'success', 'warning', 'danger'].includes(value)
    },
    size: {
      type: String,
      default: 'medium',
      validator: (value) => ['small', 'medium', 'large'].includes(value)
    },
    text: {
      type: String,
      default: ''
    },
    icon: {
      type: [String, Object],
      default: null
    },
    loading: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    block: {
      type: Boolean,
      default: false
    }
  },
  emits: ['click'],
  setup(props, { emit }) {
    const handleClick = (event) => {
      if (!props.disabled && !props.loading) {
        emit('click', event)
      }
    }

    return {
      handleClick
    }
  }
}
</script>

<style scoped>
.bx-button {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--bx-spacing-sm);
  border: 1px solid var(--bx-glass-border);
  border-radius: var(--bx-radius-large);
  background: var(--bx-glass-bg);
  backdrop-filter: var(--bx-glass-blur);
  color: var(--bx-text-primary);
  font-family: var(--bx-font-family);
  font-weight: 600;
  cursor: pointer;
  transition: all var(--bx-transition-medium);
  overflow: hidden;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  letter-spacing: -0.01em;
  outline: none;
}

.bx-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.2) 0%,
    rgba(255, 255, 255, 0.1) 100%
  );
  opacity: 0;
  transition: opacity var(--bx-transition-medium);
}

.bx-button:hover {
  transform: translateY(-2px) scale(1.02);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.2);
  background: var(--bx-glass-bg-hover);
  border-color: var(--bx-glass-border-hover);
}

.bx-button:hover::before {
  opacity: 1;
}

.bx-button:active {
  transform: translateY(-1px) scale(1.01);
}

/* 按钮尺寸 */
.bx-button--small {
  padding: 8px 16px;
  font-size: 14px;
  border-radius: var(--bx-radius-medium);
}

.bx-button--medium {
  padding: 12px 24px;
  font-size: 16px;
}

.bx-button--large {
  padding: 16px 32px;
  font-size: 18px;
}

/* 按钮类型 */
.bx-button--primary {
  background: var(--bx-primary);
  border-color: var(--bx-primary-border);
  box-shadow: 0 4px 16px var(--bx-primary-shadow);
}

.bx-button--primary::before {
  background: linear-gradient(
    135deg,
    rgba(0, 122, 255, 0.2) 0%,
    rgba(0, 122, 255, 0.1) 100%
  );
}

.bx-button--primary:hover {
  background: rgba(0, 122, 255, 0.35);
  border-color: rgba(0, 122, 255, 0.5);
  box-shadow: 0 12px 32px rgba(0, 122, 255, 0.3);
}

.bx-button--success {
  background: var(--bx-success);
  border-color: rgba(52, 199, 89, 0.4);
  box-shadow: 0 4px 16px rgba(52, 199, 89, 0.2);
}

.bx-button--warning {
  background: var(--bx-warning);
  border-color: rgba(255, 149, 0, 0.4);
  box-shadow: 0 4px 16px rgba(255, 149, 0, 0.2);
}

.bx-button--danger {
  background: var(--bx-danger);
  border-color: rgba(255, 59, 48, 0.4);
  box-shadow: 0 4px 16px rgba(255, 59, 48, 0.2);
}

/* 按钮状态 */
.bx-button--loading,
.bx-button--disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none !important;
}

.bx-button--block {
  width: 100%;
}

/* 按钮内容 */
.bx-button__icon :deep(svg) {
  width: 16px;
  height: 16px;
}

.bx-button__loading {
  display: flex;
  align-items: center;
  justify-content: center;
}

.bx-button__loading-icon {
  width: 16px;
  height: 16px;
  animation: bx-spin 1s linear infinite;
}

.bx-button__text {
  white-space: nowrap;
}

/* 加载动画 */
@keyframes bx-spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bx-button--small {
    padding: 6px 12px;
    font-size: 13px;
  }
  
  .bx-button--medium {
    padding: 10px 20px;
    font-size: 15px;
  }
  
  .bx-button--large {
    padding: 14px 28px;
    font-size: 17px;
  }
}
</style>

